/**
 * 系统设置权限设置添加管理员
 */
import './index.less'
import tem from './index.html'
import $_fn from '~plugins'
class editbumenCtrl {
  constructor($scope,$rootScope,http,$ui) {
      $scope.cancel = function () {
          $rootScope.$uiDirectiveModal.close() //关闭方法
      }
      //选择添加部门的父级部门
      console.log($scope.params)
      $scope.param1={}
      $scope.param2={
      	id:JSON.parse($scope.params).id,
      	parentId:JSON.parse($scope.params).parentId
      }
      if(JSON.parse($scope.params).id!=1){
      	console.log('请求部门数据')
      	http.post('toUpdateDept.do',{id:JSON.parse($scope.params).id},function (data) {
				if(data.errcode==1){
					console.log(data)
					$scope.param1.parents=data.data.list;
					$scope.$apply()
				}else{
					$ui.toast({
						text:data.errmsg,
						type:'warning',
						appendTo:'#editbumen'
					})
				}
	        },function(_data){
	            console.log(_data);//报错
	      	})
      }else{
      	console.log('添加一级部门');
      	$scope.param1.parents=[{name:'顶级',id:'0'}]
      }
  	//输入错误提示
  	$scope.err=function(datas){
  		$scope[datas]=true;
  		setTimeout(function() {
  			$scope[datas]=false;
  			$scope.$apply()
  		}, 1000);
  	}
  	//添加部门提交
  	  $scope.tiJiao1=function(){
  	  		console.log('添加部门')
  	  		if(!$scope.param2.name){
	  	  		$ui.toast({
					text:'请填写部门名称',
					type:'warning',
					appendTo:'#editbumen'
				})
	  	  		$scope.err('name1')
	  	  		return 
	  	  	}
  	  		http.post('updateDept.do',$scope.param2,function (data) {
				if(data.errcode==1){
					$scope.cancel()//关闭弹窗
					$ui.toast({
						text:data.errmsg,
						type:'succeed'
					})
					window.history.go(0)
				}else{
					$ui.toast({
						text:data.errmsg,
						type:'warning',
						appendTo:'#editbumen'
					})
				}
	        },function(_data){
	            console.log(_data);//报错
	      	})
  	  }
  }
}
editbumenCtrl.$inject = ['$scope','$rootScope','http','$ui']
module.exports = angular.module('directive.editbumen', []).directive('editbumen', function() {
    return {
        template: tem,
        replace:true,
        controller: editbumenCtrl,
        scope:{
        	params:"@"
        }
    }
}).name